package com.yunquan.ohana.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Rect
import android.util.AttributeSet
import com.journeyapps.barcodescanner.ViewfinderView
import com.yunquan.ohana.R

class MyViewfinderView(context: Context,attrs: AttributeSet?) : ViewfinderView(context,attrs) {
    private var backgroundBitmap: Bitmap? = null
    init {
        backgroundBitmap = BitmapFactory.decodeResource(resources, R.mipmap.ic_scan_background)
    }

    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
//        super.onDraw(canvas)
        refreshSizes()
        if (framingRect == null || previewSize == null) {
            return
        }

        val frame = framingRect
        val previewSize = this.previewSize

        val width = width
        val height = height

        // 设置圆角矩形的半径
        val cornerRadius = 30f // 圆角半径

        // Draw the exterior (i.e. outside the framing rect) darkened
//        paint.color = if (resultBitmap != null) resultColor else maskColor
        paint.color = Color.parseColor("#71000000")
        canvas.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
//        canvas.drawRect(0f, 0f, width.toFloat(), frame.top.toFloat(), paint)
//        canvas.drawRect(0f, frame.top.toFloat(), frame.left.toFloat(), (frame.bottom + 1).toFloat(), paint)
//        canvas.drawRect((frame.right + 1).toFloat(), frame.top.toFloat(), width.toFloat(), (frame.bottom + 1).toFloat(), paint)
//        canvas.drawRect(0f, (frame.bottom + 1).toFloat(), width.toFloat(), height.toFloat(), paint)
//        val path = Path()
//        path.addRoundRect(
//            frame.left.toFloat(),
//            frame.top.toFloat(),
//            width.toFloat(),
//            frame.top.toFloat(),
//            cornerRadius,
//            cornerRadius,
//            Path.Direction.CW
//        )
//        path.addRoundRect(
//            frame.right.toFloat(),
//            frame.top.toFloat(),
//            height.toFloat(),
//            frame.bottom.toFloat(),
//            cornerRadius,
//            cornerRadius,
//            Path.Direction.CW
//        )
//        path.addRoundRect(
//            frame.right.toFloat(),
//            frame.bottom.toFloat(),
//            frame.left.toFloat(),
//            frame.bottom.toFloat(),
//            cornerRadius,
//            cornerRadius,
//            Path.Direction.CW
//        )
//        path.addRoundRect(
//            frame.left.toFloat(),
//            frame.bottom.toFloat(),
//            frame.left.toFloat(),
//            frame.top.toFloat(),
//            cornerRadius,
//            cornerRadius,
//            Path.Direction.CW
//        )
//        canvas.save()
//        canvas.clipPath(path)

        if (resultBitmap != null) {
            // Draw the opaque result bitmap over the scanning rectangle
            paint.alpha = CURRENT_POINT_OPACITY
            canvas.drawBitmap(resultBitmap, null, frame, paint)
        } else {
            // If wanted, draw a red "laser scanner" line through the middle to show decoding is active
            if (laserVisibility) {
                paint.color = laserColor

                paint.alpha = SCANNER_ALPHA[scannerAlpha]
                scannerAlpha = (scannerAlpha + 1) % SCANNER_ALPHA.size

                val middle = frame.height() / 2 + frame.top
                canvas.drawRect((frame.left + 2).toFloat(), (middle - 1).toFloat(), (frame.right - 1).toFloat(), (middle + 2).toFloat(), paint)
            }

            val scaleX = this.width / previewSize.width.toFloat()
            val scaleY = this.height / previewSize.height.toFloat()

            // draw the last possible result points
            if (lastPossibleResultPoints.isNotEmpty()) {
                paint.alpha = CURRENT_POINT_OPACITY / 2
                paint.color = resultPointColor
                val radius = POINT_SIZE / 2.0f
                for (point in lastPossibleResultPoints) {
                    canvas.drawCircle(
                        (point.x * scaleX).toInt().toFloat(),
                        (point.y * scaleY).toInt().toFloat(),
                        radius, paint
                    )
                }
                lastPossibleResultPoints.clear()
            }

            // draw current possible result points
            if (possibleResultPoints.isNotEmpty()) {
                paint.alpha = CURRENT_POINT_OPACITY
                paint.color = resultPointColor
                for (point in possibleResultPoints) {
                    canvas.drawCircle(
                        (point.x * scaleX).toInt().toFloat(),
                        (point.y * scaleY).toInt().toFloat(),
                        POINT_SIZE.toFloat(), paint
                    )
                }

                // swap and clear buffers
                val temp = possibleResultPoints
                possibleResultPoints = lastPossibleResultPoints
                lastPossibleResultPoints = temp
                possibleResultPoints.clear()
            }

            // Request another update at the animation interval, but only repaint the laser line,
            // not the entire viewfinder mask.
            postInvalidateDelayed(
                ANIMATION_DELAY,
                frame.left - POINT_SIZE,
                frame.top - POINT_SIZE,
                frame.right + POINT_SIZE,
                frame.bottom + POINT_SIZE
            )
        }

        backgroundBitmap?.let {
            // 获取扫描框的宽度和高度
            val imageWidth = frame.width()
            val imageHeight = frame.height()

            val left = frame.left
            val top = frame.top
            val right = frame.right
            val bottom = frame.bottom

            val scaledBitmap = Bitmap.createScaledBitmap(it, imageWidth, imageHeight, false)
            // 创建一个矩形区域来绘制图片
            val rect = Rect(left, top, right, bottom)
            canvas.drawBitmap(scaledBitmap,null,rect,null)
        }
    }
}